package com.gxgeek.redis.application;

import com.alibaba.fastjson.JSON;
import com.gxgeek.redis.application.application.geodistance.GeoRedis;
import com.gxgeek.redis.application.application.geodistance.GeoUser;
import com.gxgeek.redis.application.redistool.JedisExecutor;
import com.gxgeek.redis.application.redistool.key.RedisKey;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.params.geo.GeoRadiusParam;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p></p>
 *
 * @since JDK 1.8
 */
@DisplayName("提升职业竞争力的必备知识 Redis 附近的人 Demo")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class GeoDemo {
    private final String key = RedisKey.GEO.genKey("tour");
    static final Logger LOG = Logger.getLogger(GeoDemo.class.getName());

    /**
     * 初始化数据
     */
    @BeforeAll
    public void init() {
        List list  =new ArrayList();
        list.add(GeoUser.builder().id("张三").longitude(116.403963).latitude(39.915119).build());
        list.add(GeoUser.builder().id("李四").longitude(116.194428).latitude(39.995692).build());
        list.add(GeoUser.builder().id("王五").longitude(116.278905).latitude(40.000224).build());
        list.add(GeoUser.builder().id("赵六").longitude(116.461225).latitude(39.925014).build());
        list.add(GeoUser.builder().id("高兴").longitude(116.300101).latitude(39.881244).build());
        GeoRedis.init(key,list);
    }

    @DisplayName("查询某个人的位置")
    @Test
    public void getOne() {
        List<GeoCoordinate> list = JedisExecutor.execute(jedis -> jedis.geopos(key, "三产", "高兴", "张三"));

        LOG.info(JSON.toJSONString(list));
        //没有信息返回 null 
        list = JedisExecutor.execute(jedis -> jedis.geopos(key, "keke"));
        LOG.info(JSON.toJSONString(list));
        list = JedisExecutor.execute(jedis -> jedis.geopos(key, "高兴"));
        LOG.info(JSON.toJSONString(list));
    }


    @DisplayName("查询某个人的到某个人的距离")
    @Test
    public void query() {
        Double result = JedisExecutor.execute(jedis -> jedis.geodist(key, "三产", "keke", GeoUnit.KM));
        LOG.info(String.valueOf(result));
        result = JedisExecutor.execute(jedis -> jedis.geodist(key, "高兴", "张三", GeoUnit.KM));
        LOG.info(String.valueOf(result));
    }

    @DisplayName("查询某个人附近的人")
    @Test
    public void around() {
        List<GeoCoordinate> list = JedisExecutor.execute(jedis -> jedis.geopos(key, "高兴"));
        GeoCoordinate geoCoordinate = list.get(0);
        double lon = geoCoordinate.getLongitude();
        double lat = geoCoordinate.getLatitude();                      //距离     // 经纬度
        GeoRadiusParam geoRadiusParam = GeoRadiusParam.geoRadiusParam().withDist().withCoord();
        List<GeoRadiusResponse> geoRadiusResponses = JedisExecutor.execute(jedis -> jedis.georadius(key, lon, lat, 100d, GeoUnit.KM,geoRadiusParam));
        LOG.info(JSON.toJSONString(geoRadiusResponses));
    }


    @DisplayName("删除某个人的位置信息")
    @Test
    public void del() {
        List<GeoCoordinate> list = JedisExecutor.execute(jedis -> {
            jedis.zrem(key, "三产");// 应为 Geo 底层 采用了zset 这种格式
            return jedis.geopos(key, "三产", "keke", "张三");
        });
        LOG.info(JSON.toJSONString(list));
    }
}
